<%--                                                                       --%>
<%-- Copyright (c) 2001-2005 BroadVision, Inc.  All rights reserved.       --%>
<%--                                                                       --%>
<%-- This software is copyrighted. Under the copyright laws, this software --%>
<%-- may not be copied, in whole or in part, without prior written consent --%>
<%-- of BroadVision, Inc. or its assignees. This software is provided      --%>
<%-- under the terms of a license between BroadVision and the recipient,   --%>
<%-- and its use is subject to the terms of that license.                  --%>
<%--                                                                       --%>
<%-- This software may be protected by one or more U.S. and International  --%>
<%-- patents.  Certain applications of BroadVision One-To-One software are --%>
<%-- covered by U.S. patent 5,710,887.                                     --%>
<%--                                                                       --%>
<%-- TRADEMARKS: BroadVision and BroadVision One-To-One are registered     --%>
<%-- trademarks of BroadVision, Inc., in the United States and the         --%>
<%-- European Community, and are trademarks of BroadVision, Inc., in other --%>
<%-- countries.  The BroadVision logo, is a trademark of BroadVision,      --%>
<%-- Inc., in the United States and other countries. Additionally, IONA    --%>
<%-- and Orbix are trademarks of IONA Technologies, Ltd.  RSA, MD5, and    --%>
<%-- RC2 are trademarks of RSA Data Security, Inc.                         --%>
<%--                                                                       --%>

<%@ page language="java" %>
<%@ page import = "java.util.Map" %>
<%@ page import = "java.util.List" %>
<%@ page import = "org.apache.struts.util.RequestUtils" %>
<%@ page import = "org.apache.commons.beanutils.DynaBean" %>
    
    
<%@ taglib uri="/WEB-INF/c.tld" prefix="c" %>

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/bv-html.tld" prefix="bvhtml" %>

<%
  // This string is present here for easy modification by JSP developer
  // Used to limit the number of messages displayed
  // the initail value is speciifed in form-beans-cs.xml
  int limitOfResultsToShow = 50;
  String limitOfResultsToShowStr = java.lang.String.valueOf(limitOfResultsToShow);
%>


<script language="javascript">

//  Trims leading blanks.
function stringLeftTrim(s) 
{
   return s.replace(/^ +/, "");
}

//  Trims trailing blanks.
function stringRightTrim(s)
{
   return s.replace(/ +$/, "");
}

//  Trims leading and trailing blanks.
function stringTrim(s)
{
   return stringRightTrim(stringLeftTrim(s));
}

//  Validate the search string and submit
function updateSearch(theForm)
{
  var searchStr = stringTrim(theForm.searchString.value);
  if (!(searchStr == "")) {
    var numOfResultsToShow =
      theForm.numOfResultsToShow.value;
    if (numOfResultsToShow < 1 ) {
      alert("<bean:message key="cs.error.relatedItems.searchLimit.positive"/>");
    }
    else if (numOfResultsToShow > <%= limitOfResultsToShow%> ) {
      alert("<bean:message key="cs.error.relatedItems.searchLimit" />");
    }
    else {
      var formpath = '<c:url value ="/cs/relatedMessageSearch.do"> <c:param name="urlParams" value="${relatedMessageView.map.urlParams}"/> </c:url>';
      theForm.submit();
      return true;
    }
  }
}

//  Populate the selected message Ids and call method on calling page
function onClickAddAndDone(theForm) {
  var itemsChecked = "";
  var total = 0;
  if (tehForm.messageKeysToAdd) {
    var len = theForm.messageKeysToAdd.length;
    if (len) {
      // more than 1 item
      for (var i = 0; i < len; i++) {
        if (theForm.messageKeysToAdd[i].checked) {
          if (0 == total) {
            itemsChecked = theForm.messageKeysToAdd[i].value;
            total = 1;
          }
          else {
            itemsChecked = itemsChecked + "," + theForm.messageKeysToAdd[i].value;
          }
        }
      } // end for
    }
    else {
      if (theForm.messageKeysToAdd.checked) {
        itemsChecked = theForm.messageKeysToAdd.value;
        total = 1;
      }
    }
  }
  if (0 < total) {
    alert("NEED TO CALL METHOD IN CALLING PAGE !!!!!");
    //window.opener.loadRelatedItems(itemsChecked, 25);
    window.close();
  }
}

function closeWindow() {
  self.close();
}

</script>

<html:form action="/cs/relatedMessageView.do">

  <%-- 
  <html:hidden property="formAction" />
  <html:hidden property="Id"/>
  <html:hidden property="currentAction"/>
  <html:hidden property="contentName"/>
  <html:hidden property="relatedDiscussionId"/>
  <html:hidden property="discussionName"/>
  --%>
  <table class="zoneFive" width="100%" border="0" 
      cellspacing="0" cellpadding="0" summary="content area">
    <tr>
      <td valign="top">

        <div class="app_pageTitle">
          <span class="left">
            <bean:message key="cs.related.items.addTo"/>&nbsp;
          </span>

          <span class="right">
            <html:button property="cancelBtn" titleKey="button.cancel" 
                styleClass="pageButton" onclick="javascript:closeWindow()">
              <bean:message key="button.cancel" />
            </html:button>

            <html:button property="addDoneBtn" titleKey="cs.find.add.done"  
                styleClass="pageButton" onclick="javascript:onClickAddAndDone(this.form);">
              <bean:message key="cs.find.add.done" />
            </html:button> 
	  </span>
        </div> 
      </td>
    </tr>

    <!-- Start Browse Items section (left column) -->
    <tr> 
    
      <!-- Category Drill down browse area (leftmost column) -->
      <td valign="top" width="40%"> 
        <table cellpadding="2" border="0" cellspacing="0" align="left" valign="top" width="99%">
          <tr> 
	    <td>
              <div class="app_pageSubtitle">
                <bean:message key="cs.related.items.discussion.browse"/>
              </div>  

              <div class="pageInstructions">
                <bean:message key="cs.related.items.discussion.instruction"/>
              </div>
            
              <div class="categoryPath"> 
                <c:url var="allDiscURL" value="/cs/relatedMessageView.do">
                  <c:param name="micrositeId" value="relatedMessageView.map.urlParams.micrositeId"/>
                  <c:param name="phaseId" value="relatedMessageView.map.urlParams.phaseId" />
                  <c:param name="currentPhaseId" value="relatedMessageView.map.urlParams.currentPhaseId" />
                  <c:param name="currentAction" value="relatedMessageView.map.urlParams.currentAction" />
                  <c:param name="messageId" value="relatedMessageView.map.urlParams.messageId" />
                </c:url>
                <%-- <a href="<c:out value="${allDiscURL}/>" /> --%>
                <html:link page="/cs/relatedMessageView.do"
                     name="relatedMessageView" property="urlParams">
                  <bean:message key="cs.related.items.discussion.all"/>
                </html:link>
              </div>
            
              <c:forEach var="dGroup" items="${relatedMessageView.map.discussionGroupList}">
                <c:if test="${dGroup.map.id != relatedMessageView.map.relatedDiscussionId}">
                  <div class="categoryListHead">
                    <a href="<html:rewrite page="/cs/relatedMessageView.do" name="dGroup" 
                        property="urlParams"/>#selected">
                      <bean:write name="dGroup" property="name" />
                    </a>
                  </div>
                </c:if>
                <c:if test="${dGroup.map.id == relatedMessageView.map.relatedDiscussionId}">
                  <div class="categoryListHeadOn">
                    <a href="<html:rewrite page="/cs/relatedMessageView.do" name="dGroup" 
                        property="urlParams"/>#selected" name="selected">
                      <bean:write name="dGroup" property="name" />
                    </a>&nbsp;
                    <html:img page="/cs/images/icons/licon_selecteditem.gif" 
                        width="8" height="18" altKey="cs.selected" border="0"/>
                  </div>
                </c:if>
              </c:forEach>
            </td>
          </tr>
        </table>
      </td>
    
      <!-- Sub-string search content item selection (middle column) -->  
      <td valign="top" width="60%"> 
        <table cellpadding="2" border="0" cellspacing="0" align="center" valign="top" width="99%">
          <tr>
            <td>
              <div class="pageSubtitle">
                <%
                  DynaBean vBean = (DynaBean)request.getAttribute("relatedMessageView"); 
                  String discName = (String)(vBean.get("discussionName")); 
                %>
                <bean:message key="cs.related.items.discussion.searchMessage" 
                    arg0= "<%=discName%>" />
              </div>
            
              <div>
                <br/>
                <span class="label" width="20%" valign="middle" nowrap>
                  <bean:message key="cs.related.items.discussion.subject" />:
                </span>
                <span width="80%" class="labelField" valign="bottom" nowrap>
                  <html:text name="csRelatedMessageForm" property="searchString" 
                      size="30" maxlength="255"/>
                  <html:button property="formAction" 
                      titleKey="cs.button.search"  
                      styleClass="titleButton" onclick="javascript:updateSearch(this.form);">
                    <bean:message key="cs.button.search" />
                  </html:button> 
                </span>
              </div>
            
              <div>
                <span width="20%">&nbsp;
                </span>
                <span width="80%" class="pageInstructions" nowrap>
                  <bean:message key="cs.related.items.discussion.subject.instruction" />
                  <br/>
                  <br/>
                </span>
              </div>
            
              <div>
                <span class="label" width="20%" valign="middle" nowrap>
                  <bean:message key="cs.related.items.limitResult" />: &nbsp;
                </span>
                <span width="80%" class="labelField " valign="bottom" nowrap >
                  <html:text name="csRelatedMessageForm" 
                     property="numOfResultsToShow" size="3" maxlength="3"/>
                  <bean:message key="cs.related.items.items" />
                </span>
              </div>
    
              <div>
                <span class="label" width="20%" valign="top">&nbsp;</span>
                <span width="80%"  class="pageInstructions" valign="top">
                  <bean:message key="cs.related.items.category.search.maximumResultLimit" 
                        arg0="<%=limitOfResultsToShowStr%>" />
                  <br/>
                  <br/>
                </span>
              </div>
            
              <%
                DynaBean viewBean = (DynaBean)request.getAttribute("relatedMessageView"); 
                List messageList = (List)(viewBean.get("messagesList")); 
                int len = messageList.size();
                String lenStr = java.lang.String.valueOf(len);
               %>
              <div id="pageSubSubtitle" class="pageSubSubtitle" height="27" colspan="2">
               <bean:message key="cs.related.items.discussion.showResults" 
                    arg0="<%=lenStr%>" />
             </div>
 
             <div class="pageInstructions" colspan="2">
               <bean:message key="cs.related.items.discussion.adddone.instruction"/>
             </div>
             <br/>
           
             <div>
               <table cellpadding="3" border="0" cellspacing="1" align="center" width="99%">
               <c:forEach var="message" items="${relatedMessageView.map.messagesList}" begin="0" end="50">
               <% //Map urlP = (Map)((DynaBean)message).get("urlParams"); 
                  //long messageId = ((Long)urlP.get("messageId")).longValue();
               %>
               <tr>    
                     <td valign="top" width="5%">  
                       <c:set var="messageId" value="message.map.urlParams.messageId"/>
                       <input type="checkbox" name="messageKeysToAdd" 
                         value="messageId">
                     </td>
                     <td align="left" valign="middle" width="95%" class="listItem">
                       <span class="label">
                         <c:out value="${message.map.title}"/>
                       </span>
                       <br/>
                       <bean:message key="cs.message.last.reply"/>: 
                       <c:out value="${message.map.lastModTime}"/>
                     </td>
                   </tr>
                 </c:forEach>
               </table>
 
             </div>
            </td>
          </tr>
        </table>
      </td>
    </tr>
    <tr>
      <td>
        <div id="pageFooter">
          <span class="right">
            <html:button property="cancelBtn" titleKey="button.cancel" 
                styleClass="pageButton" onclick="javascript:closeWindow()">
              <bean:message key="button.cancel" />
            </html:button>
 
            <html:button property="addDoneBtn" titleKey="cs.find.add.done"  
                styleClass="pageButton" onclick="javascript:onClickAddAndDone();">
              <bean:message key="cs.find.add.done" />
            </html:button> 
          </span>
        </div>
      </td>
    </tr>
  </table>
</html:form>